﻿using ScreenMonitor.Common;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using ScreenMonitor.Common.Objects;
using System.Drawing;
using System.Windows.Forms;

namespace ScreenMonitor.ClientLib
{
    public static class ClientTask
    {

        public static bool IsPauseSendBitmap { get; set; }
        /// <summary>
        /// 表示客户端发送图片的格式
        /// </summary>
        public static string BitMapFormat { get; set; }
        /// <summary>
        /// 表示客户端发送图片的大小
        /// </summary>
        public static Size BitmapSize { get; set; }
        /// <summary>
        /// 表示客户端发送图片的频率
        /// </summary>
        public static int SendBitmapRate { get; set; }
        /// <summary>
        /// 表示客户端发送图片的缩放模式
        /// </summary>
        public static PictureBoxSizeMode SendBitmapSizeMode { get; set; }
        /// <summary>
        /// 是否自动发送图片
        /// </summary>
        public static bool IsAutoStartSendImg { get; set; }
        public static void DoSendBitmap()
        {
            ImageFormat format = ImageFormat.Jpeg;
            switch (BitMapFormat.ToLower())
            {
                case "bmp":
                    format = ImageFormat.Bmp;
                    break;
                case "jpeg":
                    format = ImageFormat.Jpeg;
                    break;
                case "gif":
                    format = ImageFormat.Gif;
                    break;
                case "png":
                    format = ImageFormat.Png;
                    break;
            }
            Image scrimg=Util.ScreenToImage();
            Image genimg=Util.GenImage(scrimg,new Point(0,0),BitmapSize,SendBitmapSizeMode);
            Byte[] imgData = Util.ImageToBytes(genimg, format);
            genimg.Dispose();
            scrimg.Dispose();
            ServerObject.SendBitmap(imgData);
        }
        public static void SendBitmap()
        {
            while (true)
            {
                if (!IsPauseSendBitmap)
                {
                    DoSendBitmap();
                }
                Thread.Sleep(SendBitmapRate);
            }
        }

        public static Task SendBitMapTask = new Task(SendBitmap);


    }
}
